package com.lzc.interception;

import cn.hutool.core.util.StrUtil;
import com.lzc.util.JwtUtil;
import com.lzc.util.ThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterception implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String jwt = request.getHeader("token");
        if (StrUtil.isBlank(jwt)){
            return false;
        }
        // 获取用户id
        Long uid = JwtUtil.getUid(jwt);
        if (uid == null) {
            return false;
        }
        // 存入线程
        ThreadLocalUtil.setUid(uid);
        // 放行
        return true;
    }
}
